home *** CD-ROM | disk | FTP | other *** search
/ Internet Surfer: Getting Started / Internet Surfer - Getting Started (Wayzata Technology)(7231)(1995).bin / pc / mac / bonus / peter_le / talk_sou / my_libra / mynotifi.uni < prev    next >
Text File  |  1992-04-20  |  5KB  |  220 lines

  1. unit MyNotifier;
  2.  
  3. { This program was written by Peter N Lewis, Mar 1992 in THINK Pascal 4.0.1 }
  4.  
  5. { Derived from <jholt@adobe.COM> Joe Holt's StartupError code as posted }
  6. { to comp.sys.mac.programmer in May 1991 }
  7.  
  8. { Notification Manager messages }
  9.  
  10. { History: }
  11. {   jhh 18 jun 90 -- response to news posting }
  12. {   pnl 29 may 91 -- Converted to pascal to be used in an application }
  13.  
  14. interface
  15.  
  16.     const
  17.         mark_app = 1;
  18.         mark_none = 0;
  19.  
  20.     procedure InitNotify;
  21.     procedure FinishNotify;
  22.     procedure NotifyH (mark: integer; sound: handle; sicn: handle; str: stringPtr; display_time: longInt);
  23.     procedure Notify (mark, sound: boolean; sicn_id, sicn_index, str_id, str_index: integer; display_time: longInt);
  24. { mark - mark the current application }
  25. { sound - play sysbeep }
  26. { sicn_id, sicn_index - SICN id to rotate with the apple & index (<1 -> 1)   OR 0&0 for no sicn }
  27. { str_id, str_index - STR# id & index    OR    STR id & 0    OR    0 & 0 }
  28.     procedure NotifyIdle (foreground: boolean);
  29. { Call this in the event loop with the boolean true if you are in the foreground to remove apple mark and flash }
  30.     procedure UnNotify;
  31. { Call this to get rid of the notification }
  32.  
  33.     var
  34.         notify_finished, notify_outstanding: boolean;
  35.         time_to_unnotify: longInt;
  36.  
  37. implementation
  38.  
  39.     uses
  40.         Notification;
  41.  
  42.     const
  43.         sicn_size = 32;
  44.         T_NMInstall = $A05E;
  45.         T_Unimplemented = $A89F;
  46.  
  47.     type
  48.         NMRecPtrPtr = ^NMRecPtr;
  49.         booleanPtr = ^boolean;
  50.  
  51.     var
  52.         current_note: NMRecPtr;
  53.  
  54. { handles must be non-purgeable, but may be unlocked }
  55.  
  56. {$S Init}
  57.     procedure InitNotify;
  58.     begin
  59.         current_note := nil;
  60.         notify_finished := false;
  61.         notify_outstanding := false;
  62.         time_to_unnotify := maxLongInt;
  63.     end;
  64.  
  65. {$S}
  66.     procedure MyResponse (note: NMRecPtr);
  67.     begin
  68.         booleanPtr(note^.nmRefCon)^ := true;
  69.     end;
  70.  
  71. {$S Util}
  72.     procedure UnNotify;
  73.         var
  74.             oe: OSErr;
  75.     begin
  76.         if current_note <> nil then begin
  77.             oe := NMRemove(current_note);
  78.             with current_note^ do begin
  79.                 if nmStr <> nil then
  80.                     DisposPtr(pointer(nmStr));
  81.                 if nmIcon <> nil then
  82.                     DisposHandle(nmIcon);
  83.             end;
  84.             DisposPtr(pointer(current_note));
  85.             current_note := nil;
  86.         end;
  87.         notify_finished := false;
  88.         notify_outstanding := false;
  89.         time_to_unnotify := maxLongInt;
  90.     end;
  91.  
  92. {$S}
  93.     procedure NotifyIdle (foreground: boolean);
  94.     begin
  95.         if notify_finished and foreground or (TickCount > time_to_unnotify) then
  96.             UnNotify;
  97.     end;
  98.  
  99. {$S Term}
  100.     procedure FinishNotify;
  101.     begin
  102.         if current_note <> nil then
  103.             UnNotify;
  104.     end;
  105.  
  106. {$S Util}
  107.     procedure NotifyH (mark: integer; sound: handle; sicn: handle; str: stringPtr; display_time: longInt);
  108.         var
  109.             errorText: str255;
  110.             sh: stringHandle;
  111.             sicnH: handle;
  112.             error: boolean;
  113.             oe: OSErr;
  114.     begin
  115.         UnNotify;            { Clear outstanding notify }
  116.         if NGetTrapAddress(T_NMInstall, OSTrap) = NGetTrapAddress(T_Unimplemented, ToolTrap) then begin
  117.             SysBeep(1);   { Best we can do I guess.  Could put up the dialog box maybe?...}
  118.         end
  119.         else begin
  120.             current_note := NMRecPtr(NewPtr(sizeof(NMRec)));
  121.             if current_note = nil then begin
  122.                 SysBeep(1);   { Can't do much else if there isnt even room for this! }
  123.             end
  124.             else begin
  125.                 with current_note^ do begin
  126.                     qType := nmType;
  127.                     error := false;
  128.                     booleanPtr(nmRefCon) := @notify_finished;
  129.                     nmMark := mark;
  130.                     nmStr := str;
  131.                     nmIcon := sicn;
  132.                     nmSound := sound;
  133.                     nmResp := @MyResponse;
  134.                 end;
  135.                 oe := NMInstall(current_note);
  136.                 if oe <> noErr then begin
  137.                     current_note := nil;
  138.                     SysBeep(1);
  139.                 end
  140.                 else begin
  141.                     notify_outstanding := true;
  142.                     if display_time > 0 then
  143.                         time_to_unnotify := TickCount + display_time;
  144.                 end;
  145.             end;
  146.         end;
  147.     end;
  148.  
  149. {$S Util}
  150.     procedure Notify (mark, sound: boolean; sicn_id, sicn_index, str_id, str_index: integer; display_time: longInt);
  151.         var
  152.             errorText: str255;
  153.             sh: stringHandle;
  154.             sicnH: handle;
  155.             error: boolean;
  156.             oe: OSErr;
  157.             nmMark: integer;
  158.             nmStr: stringPtr;
  159.             nmIcon: handle;
  160.             nmSound: handle;
  161.     begin
  162.         error := false;
  163.         if mark then
  164.             nmMark := 1
  165.         else
  166.             nmMark := 0;
  167.         nmStr := nil;
  168.         if str_id <> 0 then begin
  169.             if str_index > 0 then
  170.                 GetIndString(errorText, str_id, str_index)
  171.             else begin
  172.                 errorText := '';
  173.                 sh := GetString(str_id);
  174.                 if sh <> nil then begin
  175.                     if sh^ <> nil then
  176.                         errorText := sh^^;
  177.                     ReleaseResource(handle(sh));
  178.                 end;
  179.             end;
  180.             if errorText = '' then
  181.                 error := true
  182.             else begin
  183.                 nmStr := stringPtr(NewPtr(length(errorText) + 1));
  184.                 if nmStr = nil then
  185.                     error := true
  186.                 else
  187.                     nmStr^ := errorText;
  188.             end;
  189.         end;
  190.         nmIcon := nil;
  191.         if sicn_id <> 0 then begin
  192.             if sicn_index < 1 then
  193.                 sicn_index := 1;
  194.             sicn_index := (sicn_index - 1) * sicn_size;   { 1-based, like STR# }
  195.             sicnH := GetResource('SICN', sicn_id);
  196.             HNoPurge(sicnH);
  197.             if sicnH = nil then
  198.                 error := true
  199.             else begin
  200.                 nmIcon := NewHandle(sicn_size);
  201.                 if nmIcon = nil then
  202.                     error := true
  203.                 else if nmIcon^ = nil then
  204.                     error := true
  205.                 else if GetHandleSize(sicnH) < sicn_index + sicn_size then
  206.                     error := true
  207.                 else begin
  208.                     BlockMove(ptr(longInt(sicnH^) + sicn_index), nmIcon^, sicn_size);
  209.                 end;
  210.                 ReleaseResource(sicnH);
  211.             end;
  212.         end;
  213.         if sound or error then
  214.             nmSound := handle(-1)
  215.         else
  216.             nmSound := nil;
  217.         NotifyH(nmMark, nmSound, nmIcon, nmStr, display_time);
  218.     end;
  219.  
  220. end.